﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using mojoPortal.Business;
using mojoPortal.Business.WebHelpers;
using mojoPortal.Web;

namespace blackhouse.mojoPortalExtend.Breadcrumbs {
    public class BreadcrumbsControl : UserControl {

        #region Fields

        private SiteMapDataSource sitemap;
        private SiteSettings siteSettings = null;
        private PageSettings currentPage;
        private SortedList<int, Crumb> crumbs;

        #endregion

        #region Properties

        public string StartupText { get; set; }

        #endregion

        #region Methods

        private bool SetInstance() {
            this.siteSettings = CacheHelper.GetCurrentSiteSettings();
            if (siteSettings == null) return false;

            this.currentPage = CacheHelper.GetCurrentPage();

            this.sitemap = (SiteMapDataSource)this.Page.Master.FindControl("SiteMapData");
            if (sitemap == null) return false;

            this.sitemap.SiteMapProvider = "mojosite" + siteSettings.SiteId;

            return true;
        }

        private void SetCrumbs() {
            this.crumbs = new SortedList<int, Crumb>();
            if (this.sitemap == null) return;

            BreadcrumbsAccess userCrumbs = new BreadcrumbsAccess();

            if (userCrumbs.UserSet) {
                this.crumbs = userCrumbs.GetCrumbs();
            }
            else {
                SiteMapNode currentPageNode = SiteUtils.GetCurrentPageSiteMapNode(sitemap.Provider.RootNode);

                while (currentPageNode != null) {
                    this.AddCrumb(currentPageNode);
                    currentPageNode = currentPageNode.ParentNode;
                }
                this.ReverseCrumbs();
            }
            if (this.crumbs.Count > 0) {
                this.crumbs[this.crumbs.Count - 1].IsLink = false; // ostatni (aktualna strona) jest tylko tekstem
                this.crumbs[this.crumbs.Count - 1].IsLast = true;
            }
        }

        private void ReverseCrumbs() {
            int l = this.crumbs.Count - 1;
            if (l <= 0) return; // dla zera lista ma jeden element, nie ma co odwracać

            SortedList<int, Crumb> reversed = new SortedList<int, Crumb>(l + 1);

            for (int xl = 0; xl <= l; xl++) {
                reversed.Add(xl, this.crumbs[l - xl]);
            }
            this.crumbs = reversed;
        }

        private void AddCrumb(SiteMapNode node) {
            if (node.Url != string.Empty && node.Title != string.Empty)
                this.crumbs.Add(this.crumbs.Count, new Crumb(Page.ResolveUrl(node.Url), node.Title));
        }

        protected override void Render(HtmlTextWriter writer) {
            this.SetInstance();
            this.SetCrumbs();
            /*
             Struktura:
             <ul class="breadcrumb">
             <li class="breadcrumb-li breadcrumb-startup">
                {STARTUP_TEXT}
             </li>
             <li class="breadcrumb-li breadcrumb-item">
                <a href="{NODE_URL}">
                    {NODE_NAME}
                </a>
             </li>
             <li class="breadcrumb-li breadcrumb-current">
                {LAST_NODE_NAME}
             </li>
             </ul>
             */
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "breadcrumb");
            {
                // <ul class="breadcrumb">
                writer.RenderBeginTag(HtmlTextWriterTag.Ul);
                if (!string.IsNullOrEmpty(this.StartupText)) {
                    //<li class="breadcrumb-li breadcrumb-startup">
                    writer.AddAttribute(HtmlTextWriterAttribute.Class, "breadcrumb-li breadcrumb-startup");
                    writer.RenderBeginTag(HtmlTextWriterTag.Li);
                    writer.Write(this.StartupText);
                    writer.RenderEndTag();

                }

                foreach (KeyValuePair<int, Crumb> item in this.crumbs) {
                    string classDef = "breadcrumb-li breadcrumb-item";
                    if (item.Value.IsLast)
                        classDef += " breadcrumb-current";
                    writer.AddAttribute(HtmlTextWriterAttribute.Class, classDef);
                    writer.RenderBeginTag(HtmlTextWriterTag.Li);
                    if (item.Value.IsLink) {
                        writer.AddAttribute(HtmlTextWriterAttribute.Href, item.Value.Url);
                        writer.RenderBeginTag(HtmlTextWriterTag.A);
                    }
                    writer.Write(item.Value.Name);
                    if (item.Value.IsLink)
                        writer.RenderEndTag();//</a>
                    writer.RenderEndTag();//</li>
                }

                writer.RenderEndTag(); // </ul>
            }
        }

        #endregion

    }
}
